<?php
class MessageController extends Controller
{

    public function filters()
    {
        return array(
            //'accessControl', // perform access control for CRUD operations
            array('application.components.CLoginFilter'),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array();
	}

    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('list','getdetail','getcount'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('gethomeinfo','update'),
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	public function actionList($start=0)
	{
		$query = array(
		    'limit'=>_LIMITED_PAGE_SIZE,
		    'offset'=>(int)$start,
		    'select'=>array('type','_id','desc','content'),
		);
		$message = PushMessage::model()->findAll($query);
		foreach ($message as $key => &$value) {
			$value['id'] = (string)$value['_id'];
			unset($value['_id']);
		}
		echoToMobile($message);
	}

	public function actionGetCount()
	{
		$count = PushMessage::model()->count();
		echoToMobile($count);
	}

	public function actionGetDetail($id)
	{
		$message = PushMessage::model()->findByPk(new MongoId($id));
		if(!empty($message))
			echoToMobile($message->content);
		else
			echoToError(_ERROR_SYSTEM_NO_ALLOW);
	}
}